package com.windoent.yugioh.ui.activity.meinfo;

import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baidu.mobstat.StatService;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.windoent.yugioh.R;
import com.windoent.yugioh.TestJson;
import com.windoent.yugioh.adapter.brvah.SystemNotificationAdapter;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.Notificationsbean;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.util.List;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/12 9:50
 * desc   :系统通知
 */
public class SystemNotificayionActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private SmartRefreshLayout mSmartLayout;
    private RecyclerView mRecyclerView;
    private String limit = Constants.PAGETEN;
    private int mCount = 1;
    private SystemNotificationAdapter adapter;

    @Override
    public int getLayout() {
        return R.layout.activity_sysytem_notification;
    }

    @Override
    public void initView() {
        super.initView();

        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mSmartLayout = (SmartRefreshLayout) findViewById(R.id.smart_layout);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    }

    @Override
    public void initData() {
        super.initData();
        initAdapter();
        initRefreshData("");
        fresh("");
    }

    @Override
    public void initListener() {
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initRefreshData(String text) {
        //下拉刷新
        mSmartLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                fresh(text);
            }
        });
        //加载更多
        mSmartLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadMore(text);
            }
        });
    }

    private void loadMore(String text) {
        mCount = mCount + 1;
        getSystemNotificationList(text, mCount + "", limit, false);
    }

    private void fresh(String text) {
        mCount = 1;
        getSystemNotificationList(text, mCount + "", limit, true);
    }

    private void initAdapter() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(SystemNotificayionActivity.this));
        adapter = new SystemNotificationAdapter();
        adapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.recycler_view_empty, mRecyclerView, false));
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> baseQuickAdapter, @NonNull View view, int position) {

            }
        });
    }

    //资讯列表
    private void getSystemNotificationList(String title, String page, String limit, boolean fresh) {
        showLoadingDialog("");
        PhoneLive.getMeMessageDetails(getUserToken(), "1", new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
                freshFail(fresh);
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            Notificationsbean fromJson = gson.fromJson(response, Notificationsbean.class);
                            if (fromJson != null) {
                                if (fromJson != null) {
                                    Notificationsbean.DataBean data = fromJson.getData();
                                    if (data != null) {
                                        List<Notificationsbean.DataBean.ResBean> res = data.getRes();
                                        if (res != null) {
                                            if (fresh) {
                                                adapter.setNewInstance(res);
                                                mSmartLayout.finishRefresh(true);
                                            } else {
                                                adapter.addData(res);
                                                mSmartLayout.finishLoadMore(true);
                                            }
                                        } else freshFail(fresh);
                                    } else freshFail(fresh);
                                } else freshFail(fresh);
                            } else freshFail(fresh);
                        } else freshFail(fresh);
                    } catch (JSONException e) {
                        freshFail(fresh);
                        e.printStackTrace();
                    }
                } else {
                    freshFail(fresh);
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });

    }

    //刷新失败
    private void freshFail(boolean fresh) {
        if (fresh) {
            adapter.setNewInstance(null);
            mSmartLayout.finishRefresh(false);
        } else {
            mSmartLayout.finishLoadMore(false);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "系统通知界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "系统通知界面");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }
}
